/**
 * Created by Alex on 2017/4/24.
 */
var atMenu = "首页管理";

$(".menu_header a").click(function () {

    if ($(this).text() == atMenu) {
        return;
    }
    else {
        atMenu = $(this).text();
        $(".menu_header a").each(function () {
            if ($(this).hasClass("hover")) {
                $(this).removeClass("hover");
            } else {
                $(this).addClass("hover");
            }
        });
        if (atMenu == "首页管理") {
            $(".menu_main").css("display", "block");
            $(".menu_text").css("display", "none");
        }
        else {
            $(".menu_main").css("display", "none");
            $(".menu_text").css("display", "block");
        }
    }
});

//获取版本默认值
$(function () {
    var ios = $("#iosVersionId option:selected");
    var android = $("#androidVersionId option:selected");
    $("#iosVersion").val(ios.val());
    $.post(basePath + "/version/getAndroidVersionInfo.do",{
        androidVersionId :android.val()
    },function (data) {
        if(data.status == 10001){
            $("#androidVersion").val(data.data.versionID);
            $("#androidVersionAddress").val(data.data.apkAddress);
        }else {
            swal(data.msg, "", "error");
        }
    })
});

//更新ios版本
function iosModify() {
    var iosVersion = $("#iosVersion").val();
    if(iosVersion == ""){
        swal("请输入版本号 !");
        return;
    }
    swal({
            title: "确定更新ios版本?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function(){
            $.ajax({
                type: "post",
                data: {
                    "iosVersion": iosVersion
                },
                url: basePath + "/version/updateIosVersion.do",
                success: function (data) {
                    if (data.status == 10001) {
                        swal(data.msg, "", "success");
                        $.post(basePath + "/version/getIosVersion.do",function (data) {
                            if(data.status = 10001){
                                var all_options = document.getElementById("iosVersionId").options;
                               /* for (i=0; i<all_options.length; i++){
                                    if (!all_options[i].value == iosVersion) {
                                        $("#iosVersionId").append("<option value='"+ iosVersion +"'>"+ iosVersion +"</option>");
                                    }
                                }*/
                                for (i=0; i<all_options.length; i++){
                                    if (all_options[i].value == iosVersion) {
                                        all_options[i].selected = true;
                                    }
                                }
                            }
                        })
                    }else {
                        swal(data.msg, "", "error");
                    }
                }
            })
        }
    );
}

//更新android版本
function androidModify() {
    var androidVersion = $("#androidVersion").val();
    var androidVersionAddress = $("#androidVersionAddress").val();
    if(androidVersion == ""){
        swal("请输入版本号 !");
        return;
    }
    if(androidVersionAddress == ""){
        swal("请输入版本更新地址 ! ");
        return;
    }
    swal({
            title: "确定更新android版本?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function(){
            $.ajax({
                type: "post",
                data: {
                    "androidVersion": androidVersion,
                    "androidVersionAddress" : androidVersionAddress
                },
                url: basePath + "/version/updateAndroidVersion.do",
                success: function (data) {
                    if (data.status == 10001) {
                        swal(data.msg, "", "success");
                        $.post(basePath + "/version/getAndroidVersion.do",{
                            androidVersionId : androidVersion
                        },function (data) {
                            if(data.status = 10001){
                                $("#androidVersionId").append("<option value='"+ androidVersion +"'>"+ androidVersion +"</option>");
                                var all_options = document.getElementById("androidVersionId").options;
                                for (i=0; i<all_options.length; i++){
                                    if (all_options[i].value == androidVersion) {
                                        all_options[i].selected = true;
                                    }
                                }
                            }
                        })
                    }else {
                        swal(data.msg, "", "error");
                    }
                }
            })
        }
    );
}

//获取ios信息
function selectIosVersionId() {
    var iosVersionId = $("#iosVersionId").val();
    $.ajax({
        type: "post",
        data: {
            "iosVersionId": iosVersionId
        },
        url: basePath +"/version/getIosVersionIdInfo.do",
        success: function (data) {
            if (data.status == 10001) {
                $("#iosVersion").val(data.data.versionID);
            }else {
                swal(data.msg, "", "error");
            }
        }
    })
}

//获取android信息
function selectAndroidVersionId() {
    var androidVersionId = $("#androidVersionId").val();
    $.ajax({
        type: "post",
        data: {
            "androidVersionId": androidVersionId
        },
        url: basePath + "/version/getAndroidVersionInfo.do",
        success: function (data) {
            if (data.status == 10001) {
                $("#androidVersion").val(data.data.versionID);
                $("#androidVersionAddress").val(data.data.apkAddress);
            }else {
                swal(data.msg, "", "error");
            }
        }
    })
}